package com.example.zjhuojback.mapper;


import com.example.zjhuojback.entity.Forum;
import com.example.zjhuojback.entity.Problem;
import com.example.zjhuojback.entity.User;
import com.example.zjhuojback.entity.UserProblemStatus;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProblemMapper {
    @Select("select * from `problem` where isOpen = 0 order by id asc")
    List<Problem> selectAll();

    @Select("select * from problem order by id asc ")
    List<Problem> selectAllAdmin();
    // 根据题目id搜索题目内容
    @Select("select * from `problem` where id = #{id}")
    Problem selectById(@Param("id") Integer id);

    // 用户是否过了这题
    @Select("select * from `status` where userName = #{userName} and problemId = #{problemId}")
    UserProblemStatus selectByUserAndProblemId(@Param("userName") String userName, @Param("problemId") Integer problemId);
    // 用户未过这题则插入数据
    @Insert("INSERT INTO `status` (userName, problemId) " +
            "VALUES (#{userName}, #{problemId})")
    void insertStatus(@Param("userName") String userName, @Param("problemId") Integer problemId);

    @Delete("delete from `problem` where id = #{id}")
    void deleteProblem(Integer id);

    @Insert("INSERT INTO `problem` (title, content, inputContent, outputContent, inputDescription, outputDescription, tags, judgeConfig, judgeCase, isOpen) " +
            "VALUES (#{title}, #{content}, #{inputContent}, #{outputContent}, #{inputDescription}, #{outputDescription}, #{tags}, #{judgeConfig}, #{judgeCase}, #{isOpen})")
    void insert(Problem problem);

    @Update("update `problem` set title = #{title}, content = #{content}, inputContent = #{inputContent}, " +
            "outputContent = #{outputContent}, inputDescription = #{inputDescription}, outputDescription = #{outputDescription}, tags = #{tags}, judgeConfig = #{judgeConfig}, judgeCase = #{judgeCase}, isOpen = #{isOpen} where id = #{id}")
    void updateProblem(Problem problem);

    @Update("update `problem` set isOpen = 0 where id = #{id}")
    void updateProblemIsOpen(@Param("id") Integer problemId);
    // 提交数+1
    @Update("UPDATE `problem` SET submitNum = submitNum + 1 WHERE id = #{id}")
    void updateSubmit(@Param("id") Integer id);
    // AC数+1
    @Update("UPDATE `problem` SET submitNum = submitNum + 1, acceptNum = acceptNum + 1 WHERE id = #{id}")
    void updateAccept(@Param("id") Integer id);
}
